package cn.edu.zjgsu.ito.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.edu.zjgsu.ito.service.AdminVisaManageService;
import cn.edu.zjgsu.ito.service.EduSeasonService;
import cn.edu.zjgsu.ito.service.EduStartDateService;
import cn.edu.zjgsu.ito.service.MajorNameService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class AdminVisaManageController extends Controller {
	
	/**
	 * 签证页面初始化
	 * 签证管理
	 * 杨昆鹏
	 * */
	AdminVisaManageService adminVisaManageService = ServiceFactory.createAdminVisaManageService();
	private MajorNameService majorService = ServiceFactory.createMajorNameService();
	private EduStartDateService eduStartDateService = ServiceFactory.createEduStartDateService();
	private EduSeasonService eduSeasonService = ServiceFactory.createEduSeasonService();
	private List<Record> Record1;
	private List<Record> Record2;
	private List<Record> Record3;
	private String majorName;
	private String eduStartDate;
	private String eduSeason;
	public void index() throws ParseException{
		Record1=majorService.allMajorName();	//获取所有专业
		setAttr("allMajorName",Record1);
		Record2 = eduStartDateService.allEduStartDate();//获取入学年份
		setAttr("allEduStartDate",Record2);
		Record3 = eduSeasonService.allEduSeason();//获取入学季度
		setAttr("allEduSeason",Record3);
		List<Record> stuVisa = adminVisaManageService.stuVisaManage();
		setAttr("stuVisa",stuVisa);
		//获取所有学生个人信息
		List<Record> stuInfo = adminVisaManageService.stuInfo();
		setAttr("stuInfo",stuInfo);
		render("visamanage.html");
	}
	/**
	 * 条件搜索
	 * 刘阳
	 */
	public void newsearch(){
		majorName = getPara("majorName");
		eduSeason = getPara("eduSeason");
		eduStartDate = getPara("eduStartDate");
		if((majorName==null||majorName.equals(""))&&( eduSeason==null||eduSeason.equals(""))&&(eduStartDate==null||eduStartDate.equals(""))){
			
			redirect("/toErrors");
		}
		System.out.println("$$$$$$$$$$$$$$$$$$$$$$$");
		List<Record> news= adminVisaManageService.selectNew(majorName,eduStartDate,eduSeason);
		System.out.println(news);
		String temp = null;
		temp = JsonKit.toJson(news);
		renderJson(temp);
		
	}
	//签证页面打印
	public void stuVisaPrint(){
		String passportNo = getPara("passportNo");;
		String visaTime = getPara("visaTime");
		System.out.println(passportNo+"======="+visaTime);
		int time = Integer.parseInt(visaTime);
		Record stuVisa = adminVisaManageService.stuVisaPrint(passportNo, time);
		System.out.println("姓：" + stuVisa.getStr("lastName"));
		System.out.println("名：" + stuVisa.getStr("firstName"));
		System.out.println("护照号：" + stuVisa.getStr("passportNo"));
		System.out.println("时间段：" + stuVisa.getInt("time"));
		renderJson(stuVisa);
	}
	
	//打印--专业筛选------------------------------
	public void printSerch(){
		String majorNo = getPara("majorNo");
		String eduStartDate = getPara("eduStartDate");
		String eduSeason = getPara("eduSeason");
		List<Record> temp = adminVisaManageService.printSerch(majorNo,eduStartDate,eduSeason);
		String stuInfo = "";
		stuInfo = JsonKit.toJson(temp);
		renderJson(stuInfo);
		
	}
	
	//打印页面跳转
	public void stuVisaHtml(){
		render("visa.html");
	}
	
	//签证批量上传
	public void stuVisaUpload(){
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try
		{
			UploadFile file=getFile("uploadFile");
			String fileName=file.getFileName();
			String extension=fileName.substring(fileName.lastIndexOf("."));
			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source=file.getFile();
				String newFileName=System.currentTimeMillis()+extension;
				
				System.out.println("这是啥：" + newFileName);
				
				FileInputStream fis=new FileInputStream(source);
				File targetDir=new File(filedir);
				File target=new File(targetDir,newFileName);
				OutputStream fos=new FileOutputStream(target);
				byte[] bts=new byte[300];
				while (fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				String abc = filedir+"\\"+newFileName;
				
				InputStream visaUp = new FileInputStream(abc);
				
				ArrayList<String> visaInfRet = (ArrayList<String>) adminVisaManageService.stuVisaUpload(visaUp);
				//前台交互信息
				setAttr("visaInfRet",visaInfRet);
				System.out.println(visaInfRet);
				System.out.println(newFileName);
				fos.close();
				fis.close();
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	} 
	
	//新增签证
	public void addVisa(String passportNoes,String time){
//		String passportNoes = getPara("passportNo");
//		String time = getPara("time");
			adminVisaManageService.addVisa(passportNoes,time);
	}
	
	//打印签证页面
	public void printVisa() throws ParseException{
		String passportNo = getPara("passportNo");
		String visaTime = getPara("time");
//		String passportNo = "F467784,A0202264,H2212619";
//		String visaTime = "一年";
		
		OutputStream out = null;
		Date date = new Date();
		DateFormat formatter=new SimpleDateFormat("yyyyMMddhhmmss");
		String time=formatter.format(date);
		String filedir = PathKit.getWebRootPath();
		filedir = filedir + "\\WEB-INF\\download\\";
        deleteFile(new File(filedir));//清空缓存区
		String temp = "签证办理联系单" + time +".pdf";
		String outpath = filedir + temp;
		
		try {
			out = new FileOutputStream(outpath);
			adminVisaManageService.printVisa(out,passportNo, visaTime);
			renderFile(new File(outpath));
			addVisa(passportNo,visaTime);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}	
	//模板上传
	public void fileDownload()
	{
		String fileName="留学生签证导入模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;
		renderFile(new File(path));
	}
	
	public void deleteFile(File oldPath) {
        if (oldPath.isDirectory()) {
         File[] files = oldPath.listFiles();
         for (File file : files) {
           deleteFile(file);
         }
        }else{
          oldPath.delete();
        }
	}

}
